package com.kingyun.gpsinspection.purificationservice.services.iam.outerservice.adm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.VacationApplyMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.TaskLinkIam;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.VacationApply;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.BPMService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.OutVacationApplyService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TaskLinkIamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假申请服务层
 */
@Service
public class OutVacationApplyServiceImpl implements OutVacationApplyService
{
    @Autowired
    private TaskLinkIamService taskLinkIamService;
    @Autowired
    private VacationApplyMapper applyMapper;
    @Autowired
    private BPMService bpmService;

    @Value("#{configProperties['username']}")
    private String bpmUserName;
    @Value("#{configProperties['password']}")
    private String bpmPassword;
    /**
     * 删除请假申请
     * @param vacationApplyIds
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] vacationApplyIds){
        if (vacationApplyIds.length>0){
            return  applyMapper.deleteByPrimaryKey(vacationApplyIds);
        }
        return 0;
    }

    /**
     * 新增请假申请
     * @param record
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(VacationApply record){
        //返回结果集
        int count=0;
        if(null != record){
            String username=bpmUserName;
            String password=bpmPassword;
            //开启流程
            String result= bpmService.openBPM(0,username,password,null);
            //保存任务环节
            TaskLinkIam taskLink=  new TaskLinkIam();
            taskLinkIamService.save(taskLink,result,(short)0,record.getVacationApplyId(),(short)1);
            //保存业务数据
            count+= applyMapper.insertSelective(record);
            //完成流程
            bpmService.completeTask(taskLink.getTkiid(), username, password, null);
        }
        return count;
    }

    /**
     * 根据id查询请假申请
     * @param trainingPlanId
     * @return
     */
    @Override
    public VacationApply selectByPrimaryKey(String trainingPlanId)
    {
        Map<String,Object> map = new HashMap<>();
        map.put("trainingPlanId",trainingPlanId);
        return  applyMapper.selectByPrimaryKey(map);
    }

    /**
     * 流程操作
     * @param record
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(VacationApply record,TaskLinkIam taskLinkIam,String userName,String password) {
        int count = 0;
        if(null != record){
            String parameter=null;
            short note=1;
            try{
                //判断节点位置
                System.out.println("LinkName+++++++++++"+taskLinkIam.getTaskLinkName());
                if(taskLinkIam.getTaskLinkName().equals("部门经理审核")){
                    note=2;
                    String statue = record.getStatus().toString();
                    //流程走向参数
                    parameter="{'isagree':'"+statue+"'}";
                    //转换成utf-8
                    parameter= URLEncoder.encode(parameter,"utf-8");
                    //设置任务环节审批状态
                    taskLinkIam.setAuditStatus(record.getStatus());
                    //设置任务环节备注
                    taskLinkIam.setRemark(record.getComments());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            //处理流程
            String result = bpmService.completeTask(taskLinkIam.getTkiid(), userName, password, parameter);
            System.out.println("》》》》》》》》》》》result="+result);
            //保存任务环节
            taskLinkIamService.save(taskLinkIam,result,(short)0,record.getVacationApplyId(),note);
            count+= applyMapper.updateByPrimaryKeySelective(record);
        }
        return count;
    }

    /**
     * 请假申请列表查询
     * @param vacationApply
     * @return
     */
    @Override
    public JSONObject list(VacationApply vacationApply) {
        JSONObject jsonObject = new JSONObject();
        if (vacationApply==null){
            return null;
        }
        vacationApply.setLimit(vacationApply.getLimit() == null ? Integer.valueOf(10) : vacationApply.getLimit() );
        vacationApply.setOffset(vacationApply.getOffset() == null ? Integer.valueOf(0) : vacationApply.getOffset() );
        jsonObject.put("total",applyMapper.queryCount(vacationApply));
        List<VacationApply> trainList = applyMapper.queryList(vacationApply);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }
    /**
     * 查询代处理
     * @param bootstrapSearchBean
     * @param task
     * @param type 0 休假1出差2用车
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject listToDoTask( BootstrapSearchBean bootstrapSearchBean, TaskLinkIam task, int type,String username,String password){
        JSONObject jsonObject = new JSONObject();
        try{
            jsonObject = taskLinkIamService.listToDoTask(bootstrapSearchBean,task,type,username,password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return  jsonObject;
    }

}
